<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\LiveStreaming;
use App\Models\AddressModel;
use App\Tools\LiveTools;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class LiveStreamingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new LiveStreaming(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('direct_broadcasting_title');
            $grid->column('direct_broadcasting_cover')->image();
            $grid->column('uid');
            $grid->column('start_direct_broadcasting');
            $grid->column('end_direct_broadcasting');
            $grid->column('direct_broadcasting_state','直播状态');
            $grid->column('attention');
            $grid->column('max_number_people');
            $grid->column('like_number')->sortable();
            $grid->column('','直播间')->display('进入直播间')->link(function (){
                return admin_url('lives?id='.$this->id);
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new LiveStreaming(), function (Show $show) {
            $show->field('id');
            $show->field('direct_broadcasting_title');
            $show->field('direct_broadcasting_cover');
            $show->field('uid');
            $show->field('start_direct_broadcasting');
            $show->field('end_direct_broadcasting');
            $show->field('push_url');
            $show->field('push_key');
            $show->field('live_url');
            $show->field('direct_broadcasting_state');
            $show->field('attention');
            $show->field('max_number_people');
            $show->field('like_number');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new LiveStreaming(), function (Form $form) {
            $form->display('id');
            $form->text('direct_broadcasting_title');
            $form->image('direct_broadcasting_cover');
            $form->text('uid');
            $form->datetime('start_direct_broadcasting');
            $form->radio('direct_broadcasting_state')->options([1 => '开播中', 0 => '未开播'])->default(0);
            $form->hidden('push_url');
            $form->hidden('live_url');
            $form->display('created_at');
            $form->display('updated_at');
            $form->saving(function (Form $form){
                $data = $form->input();
                if (!empty($data['uid'])){
                    $pushUrl = LiveTools::getPushUrl($data['uid']);
                    $liveUrl = LiveTools::getLivePullUrl($data['uid']);
                    $form->push_url = $pushUrl;
                    $form->live_url = $liveUrl;
                }

            });
        });
    }
}
